<template>
  <div>
    <h4>背景</h4>
    <div>有些场景，需要我们对 el-form-item 的label进行自定义处理</div>
    <div>slot 实现自定义 label</div>
    <el-form
      inline
      ref="form"
      :model="formModel"
      :rules="rules"
      label-width="140px"
      class="form-container"
    >
      <el-form-item>
        <template #label>
          <div class="label-title"><span class="label-star">*</span>姓名</div>
          <div class="label-title-tips">（姓名仅用于展示）</div>
        </template>
        <el-form-item prop="name">
          <el-input v-model="formModel.name"></el-input>
        </el-form-item>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'custom-label',
  data () {
    return {
      rules: {
        name: [
          { required: true, message: '姓名不能为空', trigger: ['change', 'blur'] }
        ]
      },
      formModel: {
        name: ''
      }
    }
  }
}
</script>

<style lang="less" scoped>
// 处理表单有星号的情景
.form-container {
  .form-label {
    display: flex;
    justify-content: flex-end;
  }
  // 使用 div元素
  .label-title {
    font-size: 14px;
    // 请直接使用 span元素
    .label-star {
      color: #f00;
      margin-right: 2px;
    }
  }

  .label-title-tips {
    font-size: 12px;
    color: #aaa;
  }
}
</style>